var t = require("../../../@babel/runtime/helpers/defineProperty"), a = getApp(), e = require("../../../utils/themeSkin/skinBehavior");

Page({
    behaviors: [ e ],
    data: {
        cityCode: "",
        city: "",
        cityName: "",
        keyword: "",
        search: "",
        searchFlag: !1,
        cities: [],
        cityData: [],
        cityArr: [],
        historyList: [],
        hostCities: [],
        timer: null
    },
    onLoad: function(t) {
        var a;
        "" !== (null !== (a = t.cityCode) && void 0 !== a ? a : "") && this.setData({
            cityCode: t.cityCode,
            city: t.city,
            cityName: decodeURIComponent(t.city)
        });
        try {
            var e = wx.getStorageSync("historyList") || [];
            e.length > 0 && this.setData({
                hostCities: JSON.parse(e)
            });
        } catch (t) {
            console.log(t);
        }
        this.setData({
            searchFlag: !1
        }), this.aroundLocation();
    },
    searchCity: function(t) {
        var a = this;
        console.log(t);
        var e = t.detail.value;
        this.setData({
            search: e
        }), "" !== this.data.search ? (this.setData({
            city: this.data.search,
            keyword: this.data.search,
            cityCode: ""
        }), this.data.timer && clearTimeout(this.data.timer), this.data.timer = setTimeout(function() {
            a.setData({
                searchFlag: !0
            }), a.aroundLocation();
        }, 100)) : this.setData({
            searchFlag: !1
        });
    },
    formattingData: function(a, e) {
        for (var i = {}, o = [], n = 0; n < a.length; n++) {
            var r = a[n];
            i[r[e]] ? i[r[e]].push(r) : i[r[e]] = [ r ];
        }
        return Object.keys(i).forEach(function(a, n) {
            var r;
            o.push((t(r = {
                index: n
            }, e, a), t(r, "list", i[a]), r));
        }), o;
    },
    filterViewMove: function() {
        return this.data.searchFlag;
    },
    aroundLocation: function() {
        var t = this, e = a.getNetAddresss("plugin.travel-around.frontend.index.index.getTravelAroundLocation"), i = {
            cityCode: this.data.cityCode,
            city: this.data.city,
            keyword: this.data.keyword
        };
        a._postNetWork({
            url: e,
            data: i,
            success: function(a) {
                var e = a.data;
                if (1 == e.result) {
                    e.data.matching || t.setData({
                        searchFlag: !1
                    });
                    var i = [];
                    wx.getStorageSync("cityCode") || "" ? i = JSON.parse(wx.getStorageSync("cityCodeArr")) || [] : (i = t.formattingData(e.data.all, "firstWord"), 
                    wx.setStorageSync("cityCodeArr", JSON.stringify(i)), wx.setStorageSync("cityCode", "firstWord")), 
                    t.setData({
                        cityData: e.data.all,
                        matching: e.data.matching || [],
                        cityArr: i
                    });
                } else wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    searchSites: function(t) {
        var a = t.currentTarget.dataset, e = a.city, i = a.citycode;
        wx.navigateTo({
            url: "/packageF/tourAround/travelIndex/travelIndex?city=".concat(e, "&cityCode=").concat(i, "&localCity=").concat(e)
        }), this.historyCity(e, i);
    },
    historyCity: function(t, a) {
        var e, i = wx.getStorageSync("historyList") || [];
        i.length > 0 && ((i = JSON.parse(i)).length >= 10 && i.pop());
        i.unshift({
            name: t,
            cityCode: a
        }), e = i.filter(function(t, a, e) {
            return e.findIndex(function(a) {
                return a.name === t.name;
            }) === a;
        }), wx.setStorageSync("historyList", JSON.stringify(e));
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});